* Before running this file please make sure the working directory in Stata
* is the same directory as this file.
* this do file requires the outreg2 package. you can install it using the
* command:
ssc install outreg2
use "alliance_replication.dta" , clear

** Create Table A1, showing all reputation scores at each alliance violation
** store as a1_table.txt

* sort data by reputation score
sort cum_vio_dum rep_dum_mem_indef

log using "a1_table.txt", text replace
* Reputation scores for each country with 1 violation
list abbrev rep_dum_mem_indef year if violate==1 & cum_vio_dum ==1, subvar

* Reputation scores for each country with 2 violations
list abbrev rep_dum_mem_indef year if violate==1 & cum_vio_dum ==2

* Reputation scores for each country with 3 violations
list abbrev rep_dum_mem_indef year if violate==1 & cum_vio_dum ==3

* Reputation scores for each country with 4 violations
list abbrev rep_dum_mem_indef year if violate==1 & cum_vio_dum ==4

* Reputation scores for each country with 5 violations
list abbrev rep_dum_mem_indef year if violate==1 & cum_vio_dum ==5

log close


** Create Regression Table A2 in appendix.
** Store in a2_table.doc
* Baseline model output
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a2_table.doc, label bdec(2) sdec(3) replace

logit alliance_gain rep_dum_mem_indef majpow democ_dum lag_num lag_numsq X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a2_table.doc, label bdec(2) sdec(3) append

* Cubic time polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a2_table.doc, label bdec(2) sdec(3) append

logit alliance_gain rep_dum_mem_indef majpow democ_dum lag_num lag_numsq t t2 t3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a2_table.doc, label bdec(2) sdec(3) append

* Cubic decay polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a2_table.doc, label bdec(2) sdec(3) append

logit alliance_gain rep_dum_mem_indef majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a2_table.doc, label bdec(2) sdec(3) append

** Create Table A3 in appendix
** store in a3_table.doc
* robustness checks requested by reviewers to add threat variable
logit alliance_gain rep_dum_mem_indef chTEAltorBmt majpow democ_dum lag_num lag_numsq X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a3_table.doc, label bdec(2) sdec(3) replace

logit alliance_gain rep_dum_mem_indef chTEAltorBmt majpow democ_dum lag_num lag_numsq t t2 t3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a3_table.doc, label bdec(2) sdec(3) append

logit alliance_gain rep_dum_mem_indef chTEAltorBmt majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a3_table.doc, label bdec(2) sdec(3) append

logit alliance_gain leaderRep chTEAltorBmt majpow democ_dum lag_num lag_numsq t t2 t3  X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a3_table.doc, label bdec(2) sdec(3) append

** Create Table A8 (Yes, this is out of order, and A4 comes next)
** store in a8_table.doc
* robustness check for reviewer 3: checking whether number of alliances in a region knocks out our variable

* Generate a variable counting the number of alliances in a region/year 
 *bysort region1 year: egen region_num = sum(number)

logit alliance_gain rep_dum_mem_indef region_num majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a8_table.doc, label bdec(2) sdec(3) replace

*********
* Check to see if one state violating in a given region/year is driving the result
* per R2's suggestion
*********

* regenerate number of alliance violations in a give year 
bysort region1 year: egen num_vio_reg_yr = sum(violate)

* Create table of how often a violation occurs in with 1-7 other violations
* Shows that single violations in a given region/year are the norm in all 
* regions but Europe
table region1 num_vio_reg_yr if  num_vio_reg_yr > 0 & violate > 0

********
* Table A4 Replicate regressions from tabel 2 just for Europe
* Show that the results hold

* Baseline model output
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq if region1==1, cluster(state)
outreg2 using a4_table.doc, label bdec(2) sdec(3) replace

logit alliance_gain rep_dum_mem_indef majpow democ_dum lag_num lag_numsq if region1==1, cluster(state)
outreg2 using a4_table.doc, label bdec(2) sdec(3) append

* Cubic time polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 if region1==1, cluster(state)
outreg2 using a4_table.doc, label bdec(2) sdec(3) append

logit alliance_gain rep_dum_mem_indef majpow democ_dum lag_num lag_numsq t t2 t3 if region1==1, cluster(state)
outreg2 using a4_table.doc, label bdec(2) sdec(3) append

* Cubic decay polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 if region1==1, cluster(state)
outreg2 using a4_table.doc, label bdec(2) sdec(3) append

logit alliance_gain rep_dum_mem_indef majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 if region1==1, cluster(state)
outreg2 using a4_table.doc, label bdec(2) sdec(3) append

*******
* Table A5 Replicate results using the regional violation rate over the previous 10 years 
* as a deflator

tsset	state year
tssmooth ma sm1 = regyrvio_avg, window(10,1,0)

gen vio_score_10yr_avg_deflator = violate/sm1
replace vio_score_10yr_avg_deflator = 0 if vio_score_10yr_avg_deflator == .

bysort state (year) : gen rep_vio_score_10yr_avg_deflator = sum(vio_score_10yr_avg_deflator)
label var rep_vio_score_10yr_avg_deflator "reputation 10yr avg deflator"

* Baseline model output
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a5_table.doc, label bdec(3) sdec(4) replace

logit alliance_gain rep_vio_score_10yr_avg_deflator majpow democ_dum lag_num lag_numsq X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a5_table.doc, label bdec(3) sdec(4) append

* Cubic time polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a5_table.doc, label bdec(3) sdec(4) append

logit alliance_gain rep_vio_score_10yr_avg_deflator majpow democ_dum lag_num lag_numsq t t2 t3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a5_table.doc, label bdec(3) sdec(4) append

* Cubic decay polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a5_table.doc, label bdec(3) sdec(4) append

logit alliance_gain rep_vio_score_10yr_avg_deflator majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a5_table.doc, label bdec(3) sdec(4) append


******
* Table A6 Replicate results using the regional violation rate over the previous 10 years as a deflator
* and restrict results to Europe

* Baseline model output
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq if region1==1, cluster(state)
outreg2 using a6_table.doc, label bdec(3) sdec(4) replace

logit alliance_gain rep_vio_score_10yr_avg_deflator majpow democ_dum lag_num lag_numsq if region1==1, cluster(state)
outreg2 using a6_table.doc, label bdec(3) sdec(4) append

* Cubic time polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 if region1==1, cluster(state)
outreg2 using a6_table.doc, label bdec(3) sdec(4) append

logit alliance_gain rep_vio_score_10yr_avg_deflator majpow democ_dum lag_num lag_numsq t t2 t3 if region1==1, cluster(state)
outreg2 using a6_table.doc, label bdec(3) sdec(4) append

* Cubic decay polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 if region1==1, cluster(state)
outreg2 using a6_table.doc, label bdec(3) sdec(4) append

logit alliance_gain rep_vio_score_10yr_avg_deflator majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 if region1==1, cluster(state)
outreg2 using a6_table.doc, label bdec(3) sdec(4) append


*****
* Merge eugene dataset to expand the years covered
* Per R1 and R2's request

use "eugene.dta", clear

rename ccode state

sort state year

drop ISOcode ISO2let ISO3let ISOShNm ISOLgNm tau_lead s_lead

save "eugene_updated_for_merge.dta", replace

use "alliance_replication.dta" , clear

merge 1:1 state year using "eugene_updated_for_merge.dta"

drop if year >1989
drop if year < 1919

* fill in region year violation average
bysort region1 (year): replace regyrvio_avg = regyrvio_avg[_n-1] if missing(regyrvio_avg) 
bysort region1 (year): replace regyrvio_avg = regyrvio_avg[_n+1] if missing(regyrvio_avg) 
replace regyrvio_avg = 0 if regyrvio_avg ==.

* fill in main variables
replace violate = 0 if violate == .
replace number = 0 if number == .
replace lag_num = 0 if lag_num == .
replace lag_numsq = 0 if lag_numsq == .
replace alliance_gain = 0 if alliance_gain == .

replace democ_dum = 1 if polity2 > 0 & democ_dum == .
replace democ_dum = 0 if polity2 <= 0 & democ_dum == .

* fill in reputation variables
bysort state (year): replace cum_vio_dum = cum_vio_dum[_n-1] if missing(cum_vio_dum)
replace cum_vio_dum = 0 if cum_vio_dum == .

bysort state (year): replace rep_dum_mem_indef = rep_dum_mem_indef[_n-1] if missing(rep_dum_mem_indef)
replace rep_dum_mem_indef = 0 if rep_dum_mem_indef == .

* fill in region fixed effects
xi i.region1
replace X_Iregion1_2 = _Iregion1_2
replace X_Iregion1_3 = _Iregion1_3
replace X_Iregion1_4 = _Iregion1_4
replace X_Iregion1_5 = _Iregion1_5
drop _Iregion1_2 _Iregion1_3 _Iregion1_4 _Iregion1_5

* Fill in decay estimate
bysort state (year): replace decay_gen = decay_gen[_n-1] + 1 if missing(decay_gen) & decay_gen[_n-1] > 0
replace decay_gen = 0 if decay_gen == .
replace decay_gensq = decay_gen^2 if decay_gensq == .
replace decay_gen3 = decay_gen^3 if decay_gen3 == .

* Fill in time polynomial
bysort state (year): replace t = t[_n-1] + 1 if missing(t) & t[_n-1] > 0
replace t = 0 if t == .
replace t2 = t^2 if t2 == .
replace t3 = t^3 if t3 == .

***********
* Create Table A7
* re-running the main regressions over all years

* Baseline model output
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a7_table.doc, label bdec(2) sdec(3) replace

logit alliance_gain rep_dum_mem_indef majpow democ_dum lag_num lag_numsq X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a7_table.doc, label bdec(2) sdec(3) append

* Cubic time polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a7_table.doc, label bdec(2) sdec(3) append

logit alliance_gain rep_dum_mem_indef majpow democ_dum lag_num lag_numsq t t2 t3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a7_table.doc, label bdec(2) sdec(3) append

* Cubic decay polynomical
logit alliance_gain cum_vio_dum majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a7_table.doc, label bdec(2) sdec(3) append

logit alliance_gain rep_dum_mem_indef majpow democ_dum lag_num lag_numsq t t2 t3 decay_gen decay_gensq decay_gen3 X_Iregion1_2 X_Iregion1_3 X_Iregion1_4 X_Iregion1_5, cluster(state)
outreg2 using a7_table.doc, label bdec(2) sdec(3) append



